<template>
	<view class="communityPage">
		<view>
			<!-- 签到 -->
			<u-overlay :show="showSignIn">
				<view class="signInPanel" v-if="!isSign">
					<view class="signTitle">
						<u--image src="/static/community/6.png" shape="square" mode="aspectFill" width="524rpx"
							height="268rpx" bgColor="transparent"></u--image>
						<view class="title">每日签到</view>
					</view>
					<view class="signInfo">
						<view>
							<view class="info">该签到了</view>
							<view class="info">签到可以获取V积分哦</view>
						</view>
						<view class="signBtn" @click="doSignIn">签到</view>
					</view>
				</view>
				<view class="signInPanel" v-else>
					<view class="signTitle">
						<u--image src="/static/community/7.png" shape="square" mode="aspectFill" width="524rpx"
							height="268rpx" bgColor="transparent"></u--image>
						<view class="title">签到成功</view>
					</view>
					<view class="signInfo">
						<view>
							<view class="info">你已经连续签到{{signInfo.continuity}}天</view>
							<view class="info">本月已经签到了{{signInfo.bmonth}}天</view>
						</view>
						<view class="signBtn" @click="goSignDetail">查看积分</view>
					</view>
				</view>
				<view class="closeSignPanel" @click="showSignIn = false">
					<u--image src="/static/community/1.png" shape="square" mode="aspectFill" width="70rpx"
						height="70rpx" bgColor="transparent"></u--image>
				</view>
			</u-overlay>
			<!-- 轮播 -->
			<view class="communityInfoSwiper">
				<u-swiper :list="list1" imgMode="aspectFill" indicator indicatorMode="dot" circular height="200rpx"
					radius="20rpx" @click="doTrial"></u-swiper>
			</view>
			<bar title="社区" :isLink="true" url="/pages/communityList/communityList"></bar>
			<view class="tabAndFilter">
				<view class="tabContent">
					<view @click="changeTab(item.id)" v-for="(item,index) in tabs" :key="index" class="tabItem"
						:class="activeTab===item.id?'isActive':''">{{item.columnName}}</view>
				</view>
				<view class="filterContent">
					<u--image src="/static/community/2.png" shape="square" mode="aspectFill" width="30rpx"
						height="30rpx" bgColor="transparent" @click="showFilterPanel=!showFilterPanel"></u--image>
					<view class="filterList" v-if="showFilterPanel">
						<view class="item" v-for="(item,index) in filters" :key="index" @click="changeFilter(item)">
							{{item.name}}
							<u--image v-if="activeFilter===item.id" src="/static/community/3.png" shape="square"
								mode="aspectFill" width="28rpx" height="18rpx" bgColor="transparent"></u--image>
						</view>
					</view>
				</view>
			</view>
			<view class="listContent">
				<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-if="!list2.length"
					text="没有更多了"></u-empty>
				<view v-for="(item,index) in list2" :key="index" class="list" @click="goDetail(item.id)">
					<view class="itemTop">
						<view class="topLeft">
							<view class="title">{{item.name}}</view>
							<view class="talk">
								<view v-for="(talk,num) in item.topicName?item.topicName.split('，'):[]" :key="num"
									class="talkItem">
									#{{talk}}
								</view>
							</view>
						</view>
						<view class="topRight">
							<u--image :src="item.images?(baseUrl+'/sys/common/static/'+item.images.split(',')[0]):''"
								shape="square" mode="aspectFill" width="208rpx" height="150rpx" bgColor="transparent"
								radius="8rpx">
							</u--image>
						</view>
					</view>
					<view class="itemBottom">
						<view class="operate">
							<view class="item">
								<u--image src="/static/community/4.png" shape="square" mode="aspectFill" width="30rpx"
									height="24rpx"></u--image>
								<view class="count">{{item.readingVolume||0}}</view>
							</view>
							<view class="item">
								<u--image src="/static/community/5.png" shape="square" mode="aspectFill" width="26rpx"
									height="26rpx"></u--image>
								<view class="count">{{item.replyCount||0}}</view>
							</view>
						</view>
						<view class="createtime">
							{{item.createTime}}
						</view>
					</view>
				</view>
			</view>
			<view @click="goMore" class="loadMoreBtn" v-if="list2.length">
				<u-loadmore :status="hasData>5?'loadmore':'nomore'" loadmore-text="加载更多" nomore-text="没有更多了"
					color="#ccc" />
			</view>
		</view>
		<tabbar :current="1"></tabbar>
	</view>
</template>

<script>
	import Tabbar from "@/components/tabbar.vue";
	import Bar from "@/components/bar.vue";
	export default {
		components: {
			Tabbar,
			Bar
		},
		data() {
			return {
				baseUrl: this.$api.baseUrl,
				userId: "",
				openId:"",
				showSignIn: false,
				signInfo: {
					continuity: 0,
					bmonth: 0
				},
				isSign: false,
				list1: [],
				list1Data: [],
				tabs: [],
				activeTab: "",
				filters: [{
					id: "1",
					name: "发布时间"
				}, {
					id: "2",
					name: "阅读量"
				}, {
					id: "3",
					name: "评论数"
				}],
				activeFilter: "1",
				showFilterPanel: false,
				list2: [],
				hasData: 0,
				isLoad: false
			}
		},
		onShow() {
			if (this.isLoad) {
				this.$request.get(this.$api.checkSignToday, {
					userId: this.userId
				}).then(signInfo => {
					uni.hideLoading()
					if (!signInfo.message) {
						this.showSignIn = true
					} else {
						this.showSignIn = false
					}
					this.initData();
				})
			}
		},
		onPullDownRefresh() {
			Promise.all([
				this.$request.get(this.$api.getBanner, {
					type: 2
				}),
				this.$request.get(this.$api.columnsList, {
					pageNo: "1",
					pageSize: "100"
				})
			]).then(res => {
				this.list1Data = res[0].result;
				this.list1 = [];
				res[0].result.map(item => {
					this.list1.push(this.$api.baseUrl + "/sys/common/static/" + item.url)
				})
				this.tabs =[];
				this.tabs =[{
					id: "",
					columnName: "推荐"
				}];
				this.tabs = this.tabs.concat(res[1].result.records)
				uni.hideLoading()
			})
			this.initData();
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.openId = userInfo.data.wxOpenId;
					this.initData();
					this.$request.get(this.$api.checkSignToday, {
						userId: userInfo.data.id
					}).then(signInfo => {
						if (!signInfo.message) {
							this.showSignIn = true
						} else {
							this.showSignIn = false
						}
						uni.hideLoading()
					})
				}
			})
			Promise.all([
				this.$request.get(this.$api.getBanner, {
					type: 2
				}),
				this.$request.get(this.$api.columnsList, {
					pageNo: "1",
					pageSize: "100"
				})
			]).then(res => {
				this.list1Data = res[0].result;
				res[0].result.map(item => {
					this.list1.push(this.$api.baseUrl + "/sys/common/static/" + item.url)
				})
				this.tabs =[{
					id: "",
					columnName: "推荐"
				}];
				this.tabs = this.tabs.concat(res[1].result.records)
				uni.hideLoading()
			})
		},
		methods: {
			goDetail(id) {
				uni.navigateTo({
					url: "/pages/postDetail/postDetail?id=" + id
				})
			},
			initData() {
				this.$request.get(this.$api.posts, {
					pageNo: "1",
					pageSize: "5",
					userId: this.userId,
					type: this.activeFilter,
					columnId: this.activeTab,
				}).then(res => {
					uni.stopPullDownRefresh()
					this.hasData = res.result.total;
					this.list2 = res.result.records;
					this.isLoad = true;
					uni.hideLoading()
				})
			},
			changeTab(id) {
				this.activeTab = id;
				this.initData();
			},
			goMore() {
				if (this.hasData <= 5) return;
				uni.navigateTo({
					url: "/pages/communityList/communityList"
				})
			},
			doTrial(index) {
				if (this.list1Data[index].link) {
					uni.navigateTo({
						url: this.list1Data[index].link+"?id="+this.list1Data[index].id
					})
				}
			},
			doSignIn() {
				this.$request.post(this.$api.doSign, {
					userId: this.userId
				}).then(res => {
					this.$request.get(this.$api.mySignInfo, {
						userId: this.userId
					}).then(result => {
						// console.log(result)
						this.signInfo = result.result;
						this.isSign = true;
						// uni.showToast({
						// 	icon: "success",
						// 	title: "签到成功"
						// })
						uni.hideLoading()
						uni.requestSubscribeMessage({
							tmplIds: ["6CRN8Jhg9wWq3WlRgMe2VppAe5La3HG_K96bBq5p0lA"], //数组
							success: (res) => {
								if (res["6CRN8Jhg9wWq3WlRgMe2VppAe5La3HG_K96bBq5p0lA"] === 'accept') {
									this.$request.post(this.$api.saveUserSign, {
										openId: this.openId
									}).then(result => {
										uni.showToast({
											title: "预约签到成功",
											icon: "success"
										})
										uni.hideLoading()
									})
								}
								if (res["6CRN8Jhg9wWq3WlRgMe2VppAe5La3HG_K96bBq5p0lA"] === 'reject') {
									uni.showToast({
										icon: "none",
										title: "已取消订阅"
									})
								}
							},
							fail: (err) => {
								if (err.errCode == 20004) {
									console.log('关闭小程序主开关')
								} else {
									uni.showToast({
										icon: "none",
										title: "订阅失败"
									})
								}
							}
						})
					})
				})
			},
			goSignDetail() {
				uni.navigateTo({
					url: "/pages/vIntegral/vIntegral"
				})
			},
			changeFilter(item) {
				this.activeFilter = item.id;
				this.showFilterPanel = false;
				this.initData();
			},
		}
	}
</script>

<style lang="scss">
	.communityPage {
		height: calc(100vh - 100rpx);
		overflow-y: auto;

		.signInPanel {
			height: 570rpx;
			width: 520rpx;
			position: relative;
			top: calc(40% - 285rpx);
			left: calc(50% - 260rpx);
			background-color: #fff;
			border-radius: 24rpx;
			overflow: hidden;

			.signTitle {
				width: 100%;
				height: 260rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				position: relative;

				.title {
					position: absolute;
					font-size: 32rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					padding-top: 40rpx;
					bottom: 40rpx;
				}
			}

			.signInfo {
				background-color: #fff;
				display: flex;
				flex-direction: column;
				justify-content: center;
				text-align: center;
				align-items: center;
				height: 310rpx;

				.info {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
					line-height: 50rpx;
				}

				.signBtn {
					margin-top: 30rpx;
					background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
					box-shadow: 0px 2px 0px 1px rgba(0, 77, 161, 1);
					border-radius: 19px 19px 19px 19px;
					height: 80rpx;
					width: 400rpx;
					line-height: 80rpx;
					color: #fff;
				}
			}
		}

		.closeSignPanel {
			background: transparent;
			position: absolute;
			bottom: 200rpx;
			left: calc(50% - 35rpx);
			padding-bottom: 80rpx;
			padding-bottom: calc(constant(safe-area-inset-bottom) + 80rpx);
			padding-bottom: calc(env(safe-area-inset-bottom) + 80rpx);
		}

		.communityInfoSwiper {
			padding: 0 40rpx;
			width: calc(100% - 80rpx);
		}

		.tabAndFilter {
			display: flex;
			justify-content: flex-start;

			.tabContent {
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				padding: 0 40rpx;
				width: calc(100vw - 160rpx);
				overflow: auto;

				.tabItem {
					background: #F4F4F4;
					border-radius: 4px 4px 4px 4px;
					color: #333333FF;
					font-size: 26rpx;
					height: 62rpx;
					padding: 0 16rpx;
					line-height: 62rpx;
					margin-right: 10rpx;
					flex-shrink: 0;
				}

				.isActive {
					color: #004DA1FF;
					background-color: #E5EDFFFF;
				}
			}

			.filterContent {
				width: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				position: relative;

				.filterList {
					display: flex;
					flex-wrap: wrap;
					flex-direction: column;
					position: absolute;
					background: #fff;
					top: 60rpx;
					right: 40rpx;
					width: 240rpx;
					background: #FFFFFF;
					box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.16);
					border-radius: 10px 10px 10px 10px;
					z-index: 9;

					.item {
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						border-bottom: 1px solid #EEEEEEFF;
						height: 100rpx;
						line-height: 100rpx;
						padding: 0 30rpx;
						display: flex;
						flex-wrap: nowrap;
						justify-content: space-between;
						align-items: center;
					}
				}
			}
		}

		.listContent {
			display: flex;
			flex-direction: column;
			padding: 20rpx 40rpx 0;

			.list {
				display: flex;
				flex-direction: column;
				padding: 30rpx 20rpx 24rpx;
				background: #FFFFFF;
				box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
				border-radius: 10px 10px 10px 10px;
				margin-bottom: 20rpx;

				.itemTop {
					display: flex;
					flex-wrap: nowrap;
					justify-content: flex-start;
					padding-bottom: 20rpx;

					.topLeft {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: 100%;

						.title {
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							font-size: 28rpx;
							letter-spacing: 1rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 3;
							-webkit-box-orient: vertical;
							padding-right: 20rpx;
						}

						.talk {
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #004DA1;
							font-size: 26rpx;
							display: flex;
							flex-wrap: nowrap;
							overflow: auto;
							margin-right: 20rpx;

							.talkItem {
								flex-shrink: 0;
							}
						}
					}

					.topRight {
						flex-shrink: 0;
					}
				}

				.itemBottom {
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					font-size: 26rpx;

					.operate {
						display: flex;
						flex-wrap: nowrap;

						.item {
							display: flex;
							flex-wrap: nowrap;
							align-items: center;
							padding-right: 40rpx;

							.count {
								padding-left: 10rpx;
							}
						}
					}
				}
			}
		}

		.loadMoreBtn {
			padding-bottom: 40rpx;
		}
	}
</style>
